﻿using PHRClient.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace PHRClient
{
    public class Patient
    {
        public int PatientId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Tel { get; set; }
        public string Gender { get; set; }
        public string Birthday { get; set; }
        public string HeightCm { get; set; }
        public string WeightKg { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        private string _age;
        private string _heightInch;
        private string _weightLb;

        public string Age
        {
            get 
            {
                DateTime now = DateTime.Today;
                DateTime birthDate = Convert.ToDateTime(this.Birthday);
                int age = now.Year - birthDate.Year;
                if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;
                return age.ToString();
            }
            set { _age = value; }
        }

        public string HeightInch
        {
            get
            {
                int hcm = Int32.Parse(this.HeightCm);
                int feet = (hcm - (hcm % 30)) / 30;
                int inch = Convert.ToInt32((hcm % 30) * 0.393701);

                _heightInch = feet + "'" + " " + inch + "\"";

                return _heightInch;
            }
            set { _heightInch = value; }
        }

        public string WeightLb
        {
            get
            {
                int wkg = Int32.Parse(this.WeightKg);
                int wlbs = Convert.ToInt32(wkg * 2.20462);
                _weightLb = wlbs.ToString();

                return _weightLb;
            }
            set { _weightLb = value; }
        }
    }

    [XmlRoot("PatientInfo")]
    public class PatientCollection : List<Patient>
    {
        public static PatientCollection Load(string fileName)
        {
            PatientCollection dc = new FileInfo(fileName).XmlDeserialize<PatientCollection>();

            return dc;
        }

        public void Save(string fileName)
        {
            this.XmlSerialize(fileName);
        }
    }

    public static class PatientLoader
    {
        public const string XMLFILE = "patients.xml";

        public static List<Patient> getAllPatients()
        {
            List<Patient> patientList = PatientCollection.Load(XMLFILE);

            return patientList;
        }

        public static int getLastInsertId()
        {
            List<Patient> patientList = PatientCollection.Load(XMLFILE);

            if (patientList.Count != 0)
                return patientList[patientList.Count-1].PatientId;
            else return 0;
        }

        public static Patient getPatientByUserNameAndPassword(string username, string password)
        {
            List<Patient> patientList = PatientCollection.Load(XMLFILE);

            Patient patient = null;

            foreach (Patient p in patientList)
            {
                if (p.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase) && p.Password.Equals(password))
                {
                    patient = p;
                }
            }

            return patient;
        }

        public static Patient getPatientByUserName(string username)
        {
            List<Patient> patientList = PatientCollection.Load(XMLFILE);

            Patient patient = null;

            foreach (Patient p in patientList)
            {
                if (p.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase))
                {
                    patient = p;
                }
            }

            return patient;
        }
    }
}
